import java.util.*;

public class shoppingCart
{
    Map<String, double[]> cart;
    Scanner scan;
    shoppingCart()
    {
        cart = new HashMap<String, double[]>();
    }

    void cartAdd(String type, double[]info)
    {
        cart.put(type, info);
    }

    void cartDel(String type)
    {
        cart.remove(type);
    }


    void cartAddUser()
    {
        System.out.print("请输入你要添加的商品名称,退出则输入quit:");
        scan = new Scanner(System.in);
        String name = scan.next();
        if (Objects.equals(name, "quit"))
            return;
        if(cart.get(name) != null)
        {
            System.out.println("添加成功，由于该商品存在你的购物车中,所以数量加1");
            return;
        }
        System.out.print("请输入你要添加的商品价格:");
        scan = new Scanner(System.in);
        double price = scan.nextDouble();
        System.out.print("请输入你要添加的商品数量:");
        scan = new Scanner(System.in);
        int num = scan.nextInt();
        cartAdd(name, new double[]{num, price});
        System.out.println("添加成功");
    }

    void cartDelUser()
    {
        while(true)
        {
            System.out.print("请输入你要删除的商品名称,退出则输入quit:");
            scan = new Scanner(System.in);
            String order = scan.next();
            if(Objects.equals(order, "quit"))
            {
                break;
            }
            else if(cart.get(order) != null)
            {
                cartDel(order);
                System.out.println(order + "删除成功!");
                break;
            }
            else
            {
                System.out.println("你输入的商品不存在你的购物车,请重新输入!");
            }
        }
    }

    // mode 为 1 计算数量 2 计算总金额
    // show 为 真打印 为假 不打印
    double calculate(int mode, boolean show)
    {
        double result = 0;
        for(Map.Entry<String, double[]> entry : cart.entrySet())
        {
            String mapKey = entry.getKey();
            double[] mapValue = entry.getValue();
            if(mode == 1)
                result += mapValue[0];
            else if(mode == 2)
                result += mapValue[1] * mapValue[0];
        }
        if(show && mode == 1)
            System.out.println("购物车商品总数为:" + (int)result + "件");
        else if(show &&  mode == 2)
            System.out.println("购物车商品总价为:" + result + "元");
        return result;
    }

    void createBill()
    {
        System.out.println("======================================================");
        System.out.println("= 你的订单:");
        System.out.printf("= %-18s%-16s%-20s\n", "商品名称", "商品价格", "商品数量");
        for(Map.Entry<String, double[]> entry : cart.entrySet())
        {
            String mapKey = entry.getKey();
            double[] mapValue = entry.getValue();
            System.out.printf("= %-20s%-20s%-20s\n", mapKey,mapValue[1], mapValue[0]);
        }
        System.out.println("= 共" + (int)calculate(1, false) + "件商品,总价为:" + calculate(2, false) + "元");
        System.out.println("======================================================");
    }

    // mode 为 true 表示增加 false 表示减少
    void changeNum(boolean mode)
    {
        if (mode)
            System.out.print("请输入你要增加的商品名称,退出则输入quit:");
        else
            System.out.print("请输入你要减少的商品名称,退出则输入quit:");
        scan = new Scanner(System.in);
        String order = scan.next();
        if(Objects.equals(order, "quit"))
        {
            return;
        }
        double[] value = cart.get(order);
        if(value == null)
        {
            System.out.println("该商品不存在你的购物车中,请添加该商品");
            return;
        }
        if(value[0] == 1 && !mode)
        {
            cartDel(order);
        }
        else
        {
            if(mode)
            {
                cartAdd(order, new double[]{value[0] + 1, value[1]});
            }
            else
            {
                cartAdd(order, new double[]{value[0] - 1, value[1]});
            }
        }
        System.out.println("操作成功");
    }


    void clear()
    {
        cart = new HashMap<String, double[]>();
    }

    void print()
    {
        System.out.println("###################################");
        System.out.println("我的购物车:");
        System.out.printf("%-18s%-16s%-20s\n", "商品名称","商品价格", "商品数量");
        for(Map.Entry<String, double[]> entry : cart.entrySet())
        {
            String mapKey = entry.getKey();
            double[] mapValue = entry.getValue();
            System.out.printf("%-20s%-20s%-20s\n", mapKey,mapValue[1], mapValue[0]);
        }
        System.out.println("###################################");
    }


    void menu()
    {
        System.out.println("###################################");
        System.out.println("# 1.查看购物车       2.添加商品      #");
        System.out.println("# 3.删除商品         4.清空购物车    #");
        System.out.println("# 5.增加商品数量     6.减少商品数量   #");
        System.out.println("# 7.计算商品数量     8.计算商品总价   #");
        System.out.println("# 9.结算购物车       0.退出          #");
        System.out.println("####################################");
        System.out.println("[@user](选择):");

    }

    public static void main(String[] args)
    {
        shoppingCart sp = new shoppingCart();
        Scanner scan;
        int order = 0;
        do
        {
            sp.menu();
            scan = new Scanner(System.in);
            order = scan.nextInt();
            switch (order) {
                case 1 -> {sp.print();break;}
                case 2 -> {sp.cartAddUser();break;}
                case 3 -> {sp.cartDelUser();break;}
                case 4 -> {sp.clear();break;}
                case 5 -> {sp.changeNum(true);break;}
                case 6 -> {sp.changeNum(false);break;}
                case 7 -> {sp.calculate(1, true);break;}
                case 8 -> {sp.calculate(2, true);break;}
                case 9 -> {sp.createBill();break;}
                case 0 -> {break;}
                default -> {System.out.println("选择错误,请重新输入");break;}
            }
        }while(order != 0);
    }

}
